package yiwang.rememberdaka;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Calendar;

import android.app.IntentService;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;

/**
 * Created by tony on 16/5/26.
 */
public class MainService extends IntentService {
    private static final String TAG = MainService.class.getSimpleName();
    private static final String CMD_MAIN = "uiautomator runtest MobileRunnerSupport.jar -c com.polaris.mobilerunner.support.Runner";

    private PowerManager pm;
    private PowerManager.WakeLock wakeLock;
    private KeyguardManager mKeyguardManager = null;
    private KeyguardLock mKeyguardLock = null;
    private Handler handler = new Handler();


    public MainService() {
        super("MainService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate");
        pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

        mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        mKeyguardLock = mKeyguardManager.newKeyguardLock("keyguard");
        mKeyguardLock.disableKeyguard();
        //点亮亮屏
        wakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
        wakeLock.acquire();

//        LogUtils.getInstance().write("onCreate, disable keyguard and wake screen");

        CheckLog.saveCheckInfo2File("屏幕唤醒时间 : " + NetWorkUtil.getInstance().getCurrentTime());
    }



    @Override
    public void onDestroy() {
        wakeLock.release();
        Log.i(TAG, "onDestory");
//        LogUtils.getInstance().write("onDestory");
//        LogUtils.getInstance().release();

        super.onDestroy();
    }



    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    protected void onHandleIntent(Intent intent) {
//        Log.i(TAG, "onHandleIntent");
//        int type = intent.getIntExtra("op_type", 0);
//        String command = CMD_MAIN;
//        if(type == 1){
//            command += "#testAppOut";
//        }else{
//            command += "#testAppIn";
//        }

//        if(conformRules()){
            NetWorkUtil.getInstance().login();

//        }


//        handler.postDelayed(new Runnable(){
//            public void run() {
//                    NetWorkUtil.getInstance().login();
//            }
//        }, 30 * 1000);
    }

    public static void execRootCommand(String command)
    {
        Process process = null;
        InputStream inputstream = null;
        DataOutputStream os = null;

        try {
            process = Runtime.getRuntime().exec("su");

            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(command + "\n");
            os.writeBytes("exit\n");
            os.flush();

            inputstream = process.getInputStream();
            InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
            BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
            String line = "";
            StringBuilder sb = new StringBuilder(line);
            while ((line = bufferedreader.readLine()) != null) {
                sb.append(line);
                sb.append('\n');
            }
            // tv.setText(sb.toString());
            Log.i(TAG, sb.toString());
//            LogUtils.getInstance().write("=============run command log\n" + sb.toString());

            process.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(os != null){
                    os.close();
                }
                if(inputstream != null){
                    inputstream.close();
                }
                if(process != null){
                    process.destroy();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }




    private boolean conformRules(){
        Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int date = c.get(Calendar.DATE);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        int second = c.get(Calendar.SECOND);

        String strCurrentTime = year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second;
        System.out.println(strCurrentTime);

        if(hour == 8 && minute <= 59){ //8点59分前可以打卡
            return true;
        }

        return false;
    }

    private static boolean isNullOrEmpty(String str) {
        return str == null || str.trim().length() == 0 || "null".equals(str);
    }





}
